# Replication code for Taylor C. Boas and Amy Erica Smith, “Looks Like Me, Thinks Like Me: Descriptive Representation and Opinion Congruence in Brazil.” Latin American Research Review 54, 2 (2019).

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file recodes mass and elite survey data and write files for analysis: one containing all elite survey data, one containing all mass survey data, and one containing all legislator-constituent dyads. It also replicates Appendix Figure 1 and one result conveyed textually in the article. We recommend running R replication files in the following order; please see readme.txt for details.
# 	1_merge_lapop.R
# 	2_merge_latinobarometro.R
# 	3_recode_reshape.R
# 	4_difference_in_distributions.R
# 	5_regressions.R
# 	6_civil_society_meeting.R
# 	7_mass_descriptives.R
# 	8_elite_descriptives.R
# 	9_mean_differences.R
#	10_difference_in_distributions_ks.R
#	11_elite_sample_simulation.R
#	12_converts_vs_lifelong.R

# Set working directory as appropriate
# setwd('~/Dropbox/brazil_leg_surveys/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.

library(Hmisc)
library(car)

rm(list=ls(all=T))

# Load data files

load('data_rosettastone.RData') 

load('brazil02.RData')
load('brazil03.RData')
load('brazil04.RData')
load('brazil05.RData')
load('brazil06.RData')
load('brazil07.RData')
load('brazil08.RData')
load('brazil09.RData')
load('brazil10.RData')
load('brazil11.RData')
load('brazil13.RData')
load('lapop_brazil07.RData')
load('lapop_brazil08.RData')
load('lapop_brazil12.RData')
load('beps_brazil10.RData')

beps1<-beps[beps$wave==1,]
beps2<-beps[beps$wave==2,]
beps3<-beps[beps$wave==3,]

beps<-merge(beps2,beps3,by='id_num',all=T)
beps<-merge(beps1,beps, by='id_num',all=T)

load('bls05augmented.RData')
load('bls09augmented.RData')
load('bls13augmented.RData')

scale01<-function(x) (x - min(x,na.rm=T))/diff(range(x,na.rm=T))

# ==========================
# Economic regime preference
# ==========================

# Recode all economic attitude variables with higher numbers being more statist. Extract factor where possible.

brazil02$intervene<-ifelse(unclass(brazil02$p22essd) %in% c(1,6), NA, unclass(brazil02$p22essd))
brazil02$marketbest<-ifelse(unclass(brazil02$p22stc) %in% c(1,6), NA, unclass(brazil02$p22stc))
brazil02$privatize<-ifelse(unclass(brazil02$p22sta) %in% c(1,6), NA, unclass(brazil02$p22sta))
econfact02<-with(brazil02,factanal(~intervene+marketbest+privatize,na.action='na.exclude',scores='regression',factors=1))
brazil02$econfact<-scale01(econfact02$scores)
brazil02$econfact<-as.vector(brazil02$econfact)

brazil03$marketdevelop<-ifelse(unclass(brazil03$p22n_f)==1,NA, unclass(brazil03$p22n_f))
brazil03$privatize<-ifelse(unclass(brazil03$p26st) %in% c(1,6), NA, unclass(brazil03$p26st))
brazil03$econfact<-scale01(brazil03$marketdevelop+ brazil03$privatize)

brazil04$privatedevelop<-ifelse(unclass(brazil04$p27nd)==1,NA, unclass(brazil04$p27nd))
brazil04$marketdevelop<-ifelse(unclass(brazil04$p22std)==1,NA, unclass(brazil04$p22std))
brazil04$econfact<-scale01(brazil04$marketdevelop+ brazil04$privatedevelop)

brazil05$privatedevelop<-ifelse(unclass(brazil05$p40ste)==1,NA, unclass(brazil05$p40ste))
brazil05$marketdevelop<-ifelse(unclass(brazil05$p25sta)==1,NA, unclass(brazil05$p25sta))
brazil05$privatize<-ifelse(unclass(brazil05$p40stc)==1,NA, unclass(brazil05$p40stc))
econfact05<-with(brazil05,factanal(~ privatedevelop+marketdevelop+privatize,na.action='na.exclude',scores='regression',factors=1))
brazil05$econfact<-scale01(econfact05$scores)
brazil05$econfact<-as.vector(brazil05$econfact)

brazil06$createwealth<-ifelse(unclass(brazil06$p16n) %in% c(1,12,13),NA, -1*unclass(brazil06$p16n))
brazil06$econfact<-scale01(brazil06$createwealth)

brazil07$marketbest<-ifelse(unclass(brazil07$p54stc) %in% c(1,6), NA, unclass(brazil07$p54stc))
brazil07$privatedevelop<-ifelse(unclass(brazil07$p54std) %in% c(1,6), NA, unclass(brazil07$p54std))
brazil07$marketdevelop<-ifelse(unclass(brazil07$p54stb) %in% c(1,6), NA, unclass(brazil07$p54stb))
brazil07$privatize<-ifelse(unclass(brazil07$p54sta) %in% c(1,6), NA, unclass(brazil07$p54sta))
econfact07<-with(brazil07,factanal(~ marketbest+privatedevelop+marketdevelop+privatize,na.action='na.exclude',scores='regression',factors=1))
brazil07$econfact<-scale01(econfact07$scores)
brazil07$econfact<-as.vector(brazil07$econfact)

brazil08$marketdevelop<-ifelse(unclass(brazil08$p57st_e)==1,NA, unclass(brazil08$p57st_e))
brazil08$econfact<-scale01(brazil08$marketdevelop)

brazil09$marketbest<-ifelse(unclass(brazil09$p81st_a) %in% c(1,6), NA, unclass(brazil09$p81st_a))
brazil09$privatedevelop<-ifelse(unclass(brazil09$p81st_b) %in% c(1,6), NA, unclass(brazil09$p81st_b))
brazil09$marketdevelop<-ifelse(unclass(brazil09$p81st_c) %in% c(1,6), NA, unclass(brazil09$p81st_c))
brazil09$privatize<-ifelse(unclass(brazil09$P81ST_D) %in% c(1,6), NA, unclass(brazil09$P81ST_D))
econfact09<-with(brazil09,factanal(~ marketbest+privatedevelop+marketdevelop+privatize,na.action='na.exclude',scores='regression',factors=1))
brazil09$econfact<-scale01(econfact09$scores)
brazil09$econfact<-as.vector(brazil09$econfact)

brazil10$privatedevelop<-ifelse(unclass(brazil10$P75ST_A) %in% c(1,6), NA, unclass(brazil10$P75ST_A))
brazil10$marketdevelop<-ifelse(unclass(brazil10$P75ST_B) %in% c(1,6), NA, unclass(brazil10$P75ST_B))
brazil10$privatize<-ifelse(unclass(brazil10$P75ST_C) %in% c(1,6), NA, unclass(brazil10$P75ST_C))
econfact10<-with(brazil10,factanal(~ privatedevelop+marketdevelop+privatize,na.action='na.exclude',scores='regression',factors=1))
brazil10$econfact<-scale01(econfact10$scores)
brazil10$econfact<-as.vector(brazil10$econfact)

brazil11$privatedevelop<-ifelse(unclass(brazil11$P69ST_A) %in% 3:4, NA, unclass(brazil11$P69ST_A))
brazil11$marketdevelop<-ifelse(unclass(brazil11$P69ST_B) %in% 3:4, NA, unclass(brazil11$P69ST_B))
brazil11$privatize<-ifelse(unclass(brazil11$P69ST_C) %in% 3:4, NA, unclass(brazil11$P69ST_C))
econfact11<-with(brazil11,factanal(~ privatedevelop+marketdevelop+privatize,na.action='na.exclude',scores='regression',factors=1))
brazil11$econfact<-scale01(econfact11$scores)
brazil11$econfact<-as.vector(brazil11$econfact)

brazil13$marketdevelop<-ifelse(unclass(brazil13$P60ST_A) %in% 3:4, NA, unclass(brazil13$P60ST_A))
brazil13$privatize<-ifelse(unclass(brazil13$P60ST_B) %in% 2:4, NA, unclass(brazil13$P60ST_B))
brazil13$econfact<-scale01(brazil13$marketdevelop+ brazil13$privatize)

lapop07$laissezfaire<-Recode(lapop07$PR9,"'Concorda muito'=1;'Concorda'=2;'Nem concorda nem discorda'=3;'Discorda'=4;'Discorda muito'=5",as.factor=F)
lapop07$econfact<-scale01(lapop07$laissezfaire)

lapop08$statefirms<-unclass(lapop08$ros1)
lapop08$statewelfare<-unclass(lapop08$ros2)
lapop08$statejobs<-unclass(lapop08$ros3)
econfact.lapop08<-with(lapop08,factanal(~ statefirms+statewelfare+statejobs,na.action='na.exclude',scores='regression',factors=1))
lapop08$econfact<-scale01(econfact.lapop08$scores)
lapop08$econfact<-as.vector(lapop08$econfact)

beps$statefirms<-unclass(beps$ros1)
beps$statewelfare<-unclass(beps$ros2)
beps$statejobs<-unclass(beps$ros3)
econfact.beps<-with(beps,factanal(~ statefirms+statewelfare+statejobs,na.action='na.exclude',scores='regression',factors=1))
beps$econfact<-scale01(econfact.beps$scores)
beps$econfact<-as.vector(beps$econfact)

lapop12$statefirms<-unclass(lapop12$ros1)
lapop12$statewelfare<-unclass(lapop12$ros2)
lapop12$statejobs<-unclass(lapop12$ros3)
econfact.lapop12<-with(lapop12,factanal(~ statefirms+statewelfare+statejobs,na.action='na.exclude',scores='regression',factors=1))
lapop12$econfact<-scale01(econfact.lapop12$scores)
lapop12$econfact<-as.vector(lapop12$econfact)

# Rescaling and recoding BLS question, collapsing category 4 into 3 (which only is picked 2 times, only in 2009)
bls05$econ.sys<-(unclass(bls05$ECONLMR) - 1)/2
bls09$econ.sys<-(ifelse(unclass(bls09$ECONLMR) == 4, 3, unclass(bls09$ECONLMR)) - 1)/2
bls13$econ.sys<-(unclass(bls13$ECONLMR) - 1)/2

# Reverse scale of BLS and LB econ questions so higher numbers are now more rightist; NA for those not using

bls05$econ.sys<-bls05$econ.sys * -1 + 1
bls09$econ.sys<-bls09$econ.sys * -1 + 1
bls13$econ.sys<-bls13$econ.sys * -1 + 1

brazil02$econfact<-brazil02$econfact * -1 + 1
brazil03$econfact<-brazil03$econfact * -1 + 1
brazil04$econfact<-brazil04$econfact * -1 + 1
brazil05$econfact<-brazil05$econfact * -1 + 1
brazil06$econfact<-brazil06$econfact * -1 + 1
brazil07$econfact<-brazil07$econfact * -1 + 1
brazil08$econfact<-brazil08$econfact * -1 + 1
brazil09$econfact<-brazil09$econfact * -1 + 1
brazil10$econfact<-brazil10$econfact * -1 + 1
brazil11$econfact<-brazil11$econfact * -1 + 1
brazil13$econfact<-brazil13$econfact * -1 + 1

lapop07$econfact<-lapop07$econfact * -1 + 1
lapop08$econfact<-NA #lapop08$econfact * -1 + 1
beps$econfact<-NA #beps$econfact * -1 + 1
lapop12$econfact<-NA #lapop12$econfact * -1 + 1

# ==========================
# Ideology
# ==========================

# Recode 0-1 with higher numbers as Right.

brazil02$ideol<-scale01(as.vector(unclass(brazil02$p64st)))
brazil03$ideol<-scale01(ifelse(brazil03$p60st > 10,NA, brazil03$p60st))
brazil04$ideol<-scale01(ifelse(brazil04$p87st > 10,NA, brazil04$p87st))
brazil05$ideol<-scale01(ifelse(brazil05$p34st > 10,NA, brazil05$p34st))
brazil06$ideol<-scale01(ifelse(as.vector(unclass(brazil06$p47st)) > 10, NA, as.vector(unclass(brazil06$p47st))))
brazil07$ideol<-scale01(ifelse(as.vector(unclass(brazil07$p67st)) > 10, NA, as.vector(unclass(brazil07$p67st))))
brazil08$ideol<-scale01(ifelse(brazil08$p56st > 10, NA, brazil08$p56st))
brazil09$ideol<-scale01(ifelse(as.vector(unclass(brazil09$p69st)) > 10, NA, as.vector(unclass(brazil09$p69st))))
brazil10$ideol<-scale01(ifelse(as.vector(unclass(brazil10$P60ST)) > 10, NA, as.vector(unclass(brazil10$P60ST))))
brazil11$ideol<-scale01(ifelse(as.vector(unclass(brazil11$P76ST)) < 6, NA, as.vector(unclass(brazil11$P76ST))))
brazil13$ideol<-scale01(ifelse(as.vector(unclass(brazil13$P41ST)) < 6, NA, as.vector(unclass(brazil13$P41ST))))

lapop07$ideol<-scale01(lapop07$L1)
lapop08$ideol<-scale01(lapop08$l1)
lapop12$ideol<-scale01(as.vector(unclass(lapop12$l1)))
beps$ideol<-scale01(ifelse(beps$l1 %in% c(88,98),NA, beps$l1))
beps$ideol2<-scale01(ifelse(beps$l1.x %in% c(88,98),NA, beps$l1.x))
beps$ideol3<-scale01(ifelse(beps$l1.y %in% c(88,98),NA, beps$l1.y))
beps$ideol[is.na(beps$ideol)]<-beps$ideol2[is.na(beps$ideol)]
beps$ideol[is.na(beps$ideol)]<-beps$ideol3[is.na(beps$ideol)]

# Rescale BLS question

bls05$ideol<-(unclass(bls05$LRCLASS) - 1)/9
bls09$ideol<-(unclass(bls09$LRCLASS) - 1)/9
bls13$ideol<-(unclass(bls13$LRCLASS) - 1)/9

# ==========================
# Gay Marriage
# ==========================

# Recode gay marriage variables on 0-1 scale with 1 indicating opposition

bls13$gaymarriage<-scale01(-1*as.vector(unclass(bls13$CASAMENT)))
bls09$gaymarriage<-NA
bls05$gaymarriage<-NA

brazil02$gaymarriage<-NA
brazil03$gaymarriage<-NA
brazil04$gaymarriage<-NA
brazil05$gaymarriage<-NA
brazil06$gaymarriage<-NA
brazil07$gaymarriage<-NA
brazil08$gaymarriage<-NA
brazil09$gaymarriage<-NA
brazil10$gaymarriage<-scale01(ifelse(as.vector(unclass(brazil10$P14N_G))==1,NA, as.vector(unclass(brazil10$P14N_G))))
brazil11$gaymarriage<-NA
brazil13$gaymarriage<-NA

lapop07$gaymarriage<-NA
lapop08$gaymarriage<-NA
lapop12$gaymarriage<-scale01(-1*as.vector(unclass(lapop12$d6)))
beps$gaymarriage<-scale01(-1*ifelse(beps$d6 > 10, NA, beps$d6))

# Not using BEPS second round Gay Marriage question because of issue with scale being reversed on the questionnaire.

# ==========================
# Affirmative Action
# ==========================

# Recode affirmative action variables on 0-1 scale with 1 indicating opposition (right-wing position)

bls13$affirm<-scale01(-1*as.vector(unclass(bls13$COTAAFRO)))
bls09$affirm<-NA
bls05$affirm<-NA

brazil02$affirm<-NA
brazil03$affirm<-NA
brazil04$affirm<-NA
brazil05$affirm<-NA
brazil06$affirm<-NA
brazil07$affirm<-NA
brazil08$affirm<-NA
brazil09$affirm<-NA
brazil10$affirm<-NA
brazil11$affirm<-NA
brazil13$affirm<-NA

lapop07$affirm<-NA
lapop08$affirm<-NA 
lapop12$affirm<-NA #scale01(-1*as.vector(unclass(lapop12$rac2a)))
beps$affirm<-scale01(ifelse(beps$rac2 %in% c(88,98),NA, -1*beps$rac2))
beps$affirm2<-scale01(ifelse(beps$rac2.x %in% c(88,98),NA, -1*beps$rac2.x))
beps$affirm3<-scale01(ifelse(beps$rac2.y %in% c(88,98),NA, -1*beps$rac2.y))
beps$affirm[is.na(beps$affirm)]<-beps$affirm2[is.na(beps$affirm)]
beps$affirm[is.na(beps$affirm)]<-beps$affirm3[is.na(beps$affirm)]

# ==========================
# Abortion
# ==========================

# Recode all variables with higher numbers indicating more opposition to abortion

bls13$abortion<-scale01(as.vector(unclass(bls13$ABORTO)))
bls09$abortion<-NA
bls05$abortion<-NA

brazil02$abortion<-NA 
brazil03$abortion<-NA
brazil04$abortion<-NA 
brazil05$abortion<-NA
brazil06$abortion<-NA
brazil07$abortion<-NA 
brazil08$abortion<-NA
brazil09$abortion<-NA
brazil10$abortion<-NA
brazil11$abortion<-NA
brazil13$abortion<-NA

lapop07$abortion<-NA
lapop08$abortion<-NA 
lapop12$abortion<-NA 
beps$abortion<-scale01(ifelse(as.vector(unclass(beps$abort.y)) %in% 5:6, NA, -1*as.vector(unclass(beps$abort.y))))

# ==========================
# Class Affirmative Action
# ==========================

# Recode class affirmative action variables on 0-1 scale with 1 indicating opposition (right-wing position)

bls13$class_affirm<-scale01(-1*as.vector(unclass(bls13$COTRENDA)))
bls09$class_affirm<-NA
bls05$class_affirm<-NA

brazil02$class_affirm<-NA
brazil03$class_affirm<-NA
brazil04$class_affirm<-NA
brazil05$class_affirm<-NA
brazil06$class_affirm<-NA
brazil07$class_affirm<-NA
brazil08$class_affirm<-NA
brazil09$class_affirm<-NA
brazil10$class_affirm<-NA
brazil11$class_affirm<-NA
brazil13$class_affirm<-NA

lapop07$class_affirm<-NA
lapop08$class_affirm<-NA 
lapop12$class_affirm<-NA
beps$class_affirm<-scale01(ifelse(beps$aanew.x %in% c(10,88,98),NA, -1*beps$aanew.x))
beps$class_affirm3<-scale01(ifelse(beps$aanew.y %in% c(10,88,98),NA, -1*beps$aanew.y))
beps$class_affirm[is.na(beps$class_affirm)]<-beps$class_affirm3[is.na(beps$class_affirm)]

# ==========================
# Environmentalism
# ==========================

# Recode environmentalism variables on 0-1 scale with 1 indicating economy and jobs over environment (right-wing position)

bls13$environment<-scale01(as.vector(unclass(bls13$ENVIRO)))
bls09$environment<-NA
bls05$environment<-NA

brazil02$environment<-NA
brazil03$environment<-NA
brazil04$environment<-NA
brazil05$environment<-NA
brazil06$environment<-NA
brazil07$environment<-NA
brazil08$environment<-NA
brazil09$environment<-NA
brazil10$environment<-NA
brazil11$environment<-scale01(-1*ifelse(as.vector(unclass(brazil11$P53ST)) %in% 1:4,NA, as.vector(unclass(brazil11$P53ST))))
brazil13$environment<-NA

lapop07$environment<-NA
lapop08$environment<-NA 
lapop12$environment<-NA
beps$environment<-NA

# ==========================
# Congruence variables
# ==========================

# Evangelical indicator

brazil02$evangelical<-ifelse(brazil02$p65st %in% c('no responde','no sabe'),NA,unclass(brazil02$p65st) %in% c(3:6,11))
brazil03$evangelical<-ifelse(brazil03$p91st %in% c('no responde','no sabe'),NA, unclass(brazil03$p91st) %in% c(3:6,11))
brazil04$evangelical<-ifelse(brazil04$p90st %in% c('no responde','no sabe'),NA, unclass(brazil04$p90st) %in% c(3:6,11))
brazil05$evangelical<-ifelse(brazil05$s2 %in% c('no responde','no sabe'),NA, unclass(brazil05$s2) %in% c(3:6,11))
brazil06$evangelical<-ifelse(brazil06$s2 %in% c('No responde','No sabe'),NA, unclass(brazil06$s2) %in% c(3:6,11))
brazil07$evangelical<-ifelse(brazil07$s4 %in% c('No responde','No sabe'),NA, unclass(brazil07$s4) %in% c(3:6,11))
brazil08$evangelical<-ifelse(brazil08$s5 %in% c('No responde','No sabe'),NA, unclass(brazil08$s5) %in% c(3:6,11))
brazil09$evangelical<-ifelse(brazil09$s7 %in% c('No responde','No sabe'),NA, unclass(brazil09$s7) %in% c(3:6,11))
brazil10$evangelical<-ifelse(brazil10$S9 %in% c('No responde','No sabe'),NA, unclass(brazil10$S9) %in% c(3:6,11))
brazil11$evangelical<-ifelse(brazil11$S18 %in% c('No contesta','No sabe'),NA, unclass(brazil11$S18) %in% c(6:9,14))
brazil13$evangelical<-ifelse(brazil13$S14 %in% c('No contesta','No sabe'),NA, unclass(brazil13$S14) %in% c(6:9,14))

lapop07$evangelical<-ifelse(is.na(lapop07$VS18), NA, unclass(lapop07$VS18) %in% 2:3)
lapop08$evangelical<-ifelse(is.na(lapop08$q3), NA, unclass(lapop08$q3) %in% c(2,5))
lapop12$evangelical<-ifelse(is.na(lapop12$q3c), NA, unclass(lapop12$q3c) %in% c(2,5))
beps$evangelical<-ifelse(is.na(beps$q3c) | beps$q3c %in% c('ns','nr'), NA, unclass(beps$q3c) %in% c(2,5))
beps$evangelical2<-ifelse(is.na(beps$q3c.x) | beps$q3c.x %in% c('ns','nr'), NA, unclass(beps$q3c.x) %in% c(2,5))
beps$evangelical3<-ifelse(is.na(beps$q3c.y) | beps$q3c.y %in% c('ns','nr'), NA, unclass(beps$q3c.y) %in% c(2,5))
beps$evangelical[is.na(beps$evangelical)]<-beps$evangelical2[is.na(beps$evangelical)]
beps$evangelical[is.na(beps$evangelical)]<-beps$evangelical3[is.na(beps$evangelical)]

# Female

brazil02$female<-toupper(brazil02$s1)=='FEMENINO'
brazil03$female<-toupper(brazil03$s1)=='FEMENINO'
brazil04$female<-toupper(brazil04$s1)=='FEMENINO'
brazil05$female<-toupper(brazil05$s6)=='FEMENINO'
brazil06$female<-toupper(brazil06$s6)=='FEMENINO'
brazil07$female<-toupper(brazil07$s10)=='FEMENINO'
brazil08$female<-toupper(brazil08$s8)=='FEMENINO'
brazil09$female<-toupper(brazil09$s5)=='FEMENINO'
brazil10$female<-toupper(brazil10$S7)=='FEMENINO'
brazil11$female<-toupper(brazil11$S16)=='FEMENINO'
brazil13$female<-toupper(brazil13$S10)=='FEMENINO'

lapop07$female<-lapop07$Q1=='Mulher'
lapop08$female<-lapop08$q1=='Mulher'
lapop12$female<-lapop12$q1=='Mulher'
beps$female<-beps$q1=='mulher'
beps$female2<-beps$q1.x=='mulher'
beps$female3<-beps$q1.y=='mulher'
beps$female[is.na(beps$female)]<-beps$female2[is.na(beps$female)]
beps$female[is.na(beps$female)]<-beps$female3[is.na(beps$female)]

bls05$female<-bls05$sex=='FEMININO'
bls09$female<-bls09$sex=='FEMININO'
bls13$female<-bls13$sex=='FEMININO'

# Black

brazil02$black<-NA
brazil03$black<-NA
brazil04$black<-NA
brazil05$black<-NA
brazil06$black<-NA 
brazil07$black<-ifelse(brazil07$s9 %in% c('No responde','No sabe'),NA, brazil07$s9 %in% c('Negro(a)','Mestizo(a)','Mulato(a)'))
brazil08$black<-ifelse(brazil08$s11 %in% c('No responde','No sabe'),NA, brazil08$s11 %in% c('Negro(a)','Mestizo(a)','Mulato(a)'))
brazil09$black<-ifelse(brazil09$s18 %in% c('No responde','No sabe'),NA, brazil09$s18 %in% c('Negro(a)','Mestizo(a)','Mulato(a)'))
brazil10$black<-ifelse(brazil10$S20 %in% c('No responde','No sabe'),NA, brazil10$S20 %in% c('Negro(a)','Mestizo(a)','Mulato(a)'))
brazil11$black<-ifelse(brazil11$S27 %in% c('No contesta','No sabe'),NA, brazil11$S27 %in% c('Negro(a)','Mestizo(a)','Mulato(a)'))
brazil13$black<-ifelse(brazil13$S21 %in% c('No responde','No sabe'),NA, brazil13$S21 %in% c('Negro','Mestizo','Mulato'))

lapop07$black<-ifelse(is.na(lapop07$VS20), lapop07$VS20b, lapop07$VS20) %in% 1:2
lapop08$black<-ifelse(is.na(lapop08$etid), NA, lapop08$etid %in% c('Pardo','Preta'))
lapop12$black<-ifelse(is.na(lapop12$etid), NA, lapop12$etid %in% c('Pardo','Preta'))
beps$black<-ifelse(is.na(beps$etid) | beps$etid %in% c('ns','nr'), NA, beps$etid %in% c('pardo','preta'))
beps$black2<-ifelse(is.na(beps$etid.x) | beps$etid.x %in% c('ns','nr'), NA, beps$etid.x %in% c('pardo','preta'))
beps$black3<-ifelse(is.na(beps$etid.y) | beps$etid.y %in% c('ns','nr'), NA, beps$etid.y %in% c('pardo','preta'))
beps$black[is.na(beps$black)]<-beps$black2[is.na(beps$black)]
beps$black[is.na(beps$black)]<-beps$black3[is.na(beps$black)]

# Black in BLS: self-identified as preto or pardo in 2014, or for those who did not run in 2014, listed as Black in UNEGRO 2011 or Santos 2010.

bls09$black<-bls09$race %in% c('PRETA','PARDA') | (is.na(bls09$race) & bls09$black)
bls13$black<-bls13$race %in% c('PRETA','PARDA') | (is.na(bls13$race) & bls13$black)

# Low (no college) education

brazil02$low.educ<-unclass(brazil02$s6) < 14
brazil03$low.educ<-unclass(brazil03$s6) < 14
brazil04$low.educ<-unclass(brazil04$s6) < 14
brazil05$low.educ<-unclass(brazil05$s11) < 14
brazil06$low.educ<-unclass(brazil06$s11) < 14
brazil07$low.educ<-unclass(brazil07$s15) < 15
brazil08$low.educ<-unclass(brazil08$s15) < 15
brazil09$low.educ<-unclass(brazil09$s12) < 15
brazil10$low.educ<-unclass(brazil10$S14) < 15
brazil11$low.educ<-unclass(brazil11$S21) < 18
brazil13$low.educ<-unclass(brazil13$S17) < 18

lapop07$low.educ<-unclass(lapop07$VS2) < 16
lapop08$low.educ<-unclass(lapop08$ed) < 13
lapop12$low.educ<-unclass(lapop12$ed) < 12
beps$low.educ<-ifelse(beps$ed %in% c(88,98),NA, beps$ed < 12)
beps$low.educ2<-ifelse(beps$ed.x %in% c(88,98),NA, beps$ed.x < 12)
beps$low.educ3<-ifelse(beps$ed.y %in% c(88,98),NA, beps$ed.y < 12)
beps$low.educ[is.na(beps$low.educ)]<-beps$low.educ2[is.na(beps$low.educ)]
beps$low.educ[is.na(beps$low.educ)]<-beps$low.educ3[is.na(beps$low.educ)]

bls05$low.educ<-!bls05$educ %in% c('SUPERIOR COMPLETO','SUPERIOR INCOMPLETO')
bls05$low.educ[is.na(bls05$educ) | bls05$educ == 'NÃO INFORMADO']<-NA
bls09$low.educ<-!bls09$educ %in% c('SUPERIOR COMPLETO','SUPERIOR INCOMPLETO')
bls13$low.educ<-!bls13$educ %in% c('SUPERIOR COMPLETO','SUPERIOR INCOMPLETO')

# Party you would vote for / identify with

brazil02$party<-Recode(iconv(brazil02$p45st,from='latin1'),"'partido do movimento democrático brasileiro'='PMDB'; 'partido da frente liberal'='PFL'; 'partido da social democracia brasileira'='PSDB'; 'partido dos trabalhadores'='PT'; 'partido trabalhista brasileiro'='PTB'; 'partido demócrática trabalhista'='PDT'; 'partido progressista brasileiro'='PPB'; 'partido socialista brasileiro'='PSB'; 'partido popular socialista'='PPS'; 'partido liberal (brasil)'='PL';else=NA")
brazil03$party<-Recode(brazil03$p54st,"1='PT';2='PL';3='PTB';4='PPS';5='PSB';6='PMDB';7='PSDB';8='PFL';9='PDT';10='PP';else=NA") # Labels obtained by comparing weighted frequencies in file to frequencies with labels in LB online analysis tool
brazil04$party<-NA # LB 2004 labels for Brazil party are not believable; there is no one who chose the PT or PMDB, and around 15% choose the PDT. Sent an email asking for confirmation; lacking a solution, coding as NA.
brazil05$party<-gsub('.*\\((P.*)\\)$','\\1', iconv(brazil05$p48st,from='latin1'))
brazil05$party[grep('No|Otros|nulo',brazil05$party)]<-NA
brazil05$party[brazil05$party=='BR: Partido de Reedificação da Ordem Nacional']<-'PRONA'
brazil06$party<-gsub('.*\\((P.*)\\)$','\\1', iconv(brazil06$p38st,from='latin1'))
brazil06$party[grep('No|Otros|nulo',brazil06$party)]<-NA
brazil07$party<-gsub('.*\\(([PD].*)\\)$','\\1', iconv(brazil07$p64st,from='latin1'))
brazil07$party[grep('No|Otros|nulo',brazil07$party)]<-NA
brazil07$party[brazil07$party=='DEM) (ex-PFL']<-'DEM'
brazil08$party<-gsub('.*\\(([PD].*)\\)$','\\1', iconv(brazil08$p61st,from='latin1'))
brazil08$party[grep('No|Otros|nulo',brazil08$party)]<-NA
brazil08$party[brazil08$party=='DEM) (ex-PFL']<-'DEM'
brazil09$party<-gsub('.*\\(([PD].*)\\)$','\\1', iconv(brazil09$p35st,from='latin1'))
brazil09$party[grep('No|Otros|nulo',brazil09$party)]<-NA
brazil09$party[brazil09$party=='DEM) (ex-PFL']<-'DEM'
brazil10$party<-gsub('.*\\(([PD].*)\\)$','\\1', iconv(brazil10$P29ST,from='latin1'))
brazil10$party[grep('No|Otro|nulo',brazil10$party)]<-NA
brazil10$party[brazil10$party=='DEM) (ex-PFL']<-'DEM'
brazil11$party<-gsub('.*\\(([PD].*)\\)$','\\1', iconv(brazil11$P38ST,from='latin1'))
brazil11$party[grep('No|Otro|nulo',brazil11$party)]<-NA
brazil11$party[brazil11$party=='DEM) (ex-PFL']<-'DEM'
brazil13$party<-gsub('.*\\(([PD].*)\\)$','\\1', brazil13$P22TGBSM)
brazil13$party[grep('No|nulo',brazil13$party)]<-NA
brazil13$party[brazil13$party=='DEM) (ex-PFL']<-'DEM'

lapop07$party<-gsub('(.*) \\-.*','\\1', lapop07$SEP2)
lapop07$party[lapop07$party=='NS']<-NA
lapop08$party<-as.character(lapop08$vb11)
lapop08$party[lapop08$party %in% c('Nenhum','Outro')]<-NA
lapop12$party<-toupper(as.character(lapop12$vb11))
lapop12$party<-gsub('\\(.*\\) ','', lapop12$party)
lapop12$party[lapop12$party == 'OUTRO']<-NA
lapop12$party[lapop12$party == 'DEMOCRATAS']<-'DEM'
lapop12$party[lapop12$party == 'PCDOB']<-'PC do B'

beps$party<-factor(beps$vb11, levels=c(1501:1514,88,98,99), labels=c('PT','PMDB','PSDB','PSB','DEM','PC do B','PPS','PTB','PSOL','PP','PL','PV','PDT','OUTRO','NS','NR','NA'))
beps$party[beps$party == 'OUTRO']<-NA
beps$party[beps$party %in% c('NS','NR','NA')]<-NA

beps$party2<-factor(beps$vb11.x, levels=c(1501:1514,88,98,99), labels=c('PT','PMDB','PSDB','PSB','DEM','PC do B','PPS','PTB','PSOL','PP','PL','PV','PDT','OUTRO','NS','NR','NA'))
beps$party2[beps$party2 == 'OUTRO']<-NA
beps$party2[beps$party2 %in% c('NS','NR','NA')]<-NA

beps$party3<-factor(beps$vb11.y, levels=c(1501:1514,88,98,99), labels=c('PT','PMDB','PSDB','PSB','DEM','PC do B','PPS','PTB','PSOL','PP','PL','PV','PDT','OUTRO','NS','NR','NA'))
beps$party3[beps$party3 == 'OUTRO']<-NA
beps$party3[beps$party3 %in% c('NS','NR','NA')]<-NA

beps$party[is.na(beps$party)]<-beps$party2[is.na(beps$party)]
beps$party[is.na(beps$party)]<-beps$party3[is.na(beps$party)]
beps$party<-as.vector(beps$party)

# UF (needs merging across waves for BEPS)

beps$uf[is.na(beps$uf)]<-beps$uf.x[is.na(beps$uf)]
beps$uf[is.na(beps$uf)]<-beps$uf.y[is.na(beps$uf)]

# Respondent unique identifiers

brazil02$unique<-paste('lb02',1:nrow(brazil02),sep='-')
brazil03$unique<-paste('lb03',1:nrow(brazil03),sep='-')
brazil04$unique<-paste('lb04',1:nrow(brazil04),sep='-')
brazil05$unique<-paste('lb05',1:nrow(brazil05),sep='-')
brazil06$unique<-paste('lb06',1:nrow(brazil06),sep='-')
brazil07$unique<-paste('lb07',1:nrow(brazil07),sep='-')
brazil08$unique<-paste('lb08',1:nrow(brazil08),sep='-')
brazil09$unique<-paste('lb09',1:nrow(brazil09),sep='-')
brazil10$unique<-paste('lb10',1:nrow(brazil10),sep='-')
brazil11$unique<-paste('lb11',1:nrow(brazil11),sep='-')
brazil13$unique<-paste('lb13',1:nrow(brazil13),sep='-')

lapop07$unique<-paste('lapop07',1:nrow(lapop07),sep='-')
lapop08$unique<-paste('lapop08',1:nrow(lapop08),sep='-')
lapop12$unique<-paste('lapop12',1:nrow(lapop12),sep='-')
beps$unique<-paste('beps',beps$id_num,sep='-')

# BLS year identifiers

brazil02$blsyear <-2005
brazil03$blsyear <-2005
brazil04$blsyear <-2005
brazil05$blsyear <-2005
brazil06$blsyear <-2009
brazil07$blsyear <-2009
brazil08$blsyear <-2009
brazil09$blsyear <-2009
brazil10$blsyear <-2013
brazil11$blsyear <-2013
brazil13$blsyear <-2013

lapop07$blsyear <-2009
lapop08$blsyear <-2009
lapop12$blsyear <-2013
beps$blsyear <-2013

# ==========================
# Control variables--mass
# ==========================

# Weights to deal with over/undersampling by region in BEPS 2010 and AB 2012

brazil02$pweight<-brazil03$pweight<-brazil04$pweight<-brazil05$pweight<-brazil06$pweight<-brazil07$pweight<-brazil08$pweight<-brazil09$pweight<-brazil10$pweight<-brazil11$pweight<-brazil13$pweight<-lapop07$pweight<-lapop08$pweight<-1

region.pop<-read.csv('region2010.csv',as.is=T)
region.pop<-apply(region.pop[2:6],2,sum)
region.pct.pop<-data.frame(region=c('North','Northeast','Southeast','South','Center-West'),pop_pct=100*prop.table(region.pop))

beps.uf<-data.frame(100*prop.table(table(beps$uf)))
names(beps.uf)<-c('uf','pct')
beps.uf$region<-factor(1*(beps.uf$uf %in% c('PR','RS','SC'))+2*(beps.uf$uf %in% c('SP','RJ','ES','MG'))+3*(beps.uf$uf %in% c('AM','RR','AP','PA','TO','RO','AC'))+4*(beps.uf$uf %in% c('MA','PI','CE','RN','PE','PB','SE','AL','BA'))+5*(beps.uf$uf %in% c('MT','MS','GO','DF')),labels=c('South','Southeast','North','Northeast','Center-West'))
beps.region<-with(beps.uf,aggregate(list(pct=pct),list(region=region),sum))
beps.region<-merge(beps.region,region.pct.pop)
beps.region$pweight<-beps.region$pop_pct/beps.region$pct
beps.uf<-merge(beps.uf,beps.region[,c('region','pweight')])
beps<-merge(beps,beps.uf[,c('uf','pweight')])

lapop12.uf<-data.frame(100*prop.table(table(lapop12$uf)))
names(lapop12.uf)<-c('uf','pct')
lapop12.uf$region<-factor(1*(lapop12.uf$uf %in% c('PR','RS','SC'))+2*(lapop12.uf$uf %in% c('SP','RJ','ES','MG'))+3*(lapop12.uf$uf %in% c('AM','RR','AP','PA','TO','RO','AC'))+4*(lapop12.uf$uf %in% c('MA','PI','CE','RN','PE','PB','SE','AL','BA'))+5*(lapop12.uf$uf %in% c('MT','MS','GO','DF')),labels=c('South','Southeast','North','Northeast','Center-West'))
lapop12.region<-with(lapop12.uf,aggregate(list(pct=pct),list(region=region),sum))
lapop12.region<-merge(lapop12.region,region.pct.pop)
lapop12.region$pweight<-lapop12.region$pop_pct/lapop12.region$pct
lapop12.uf<-merge(lapop12.uf,lapop12.region[,c('region','pweight')])
lapop12<-merge(lapop12,lapop12.uf[,c('uf','pweight')])

# Age

brazil02$age<-brazil02$s2
brazil03$age<-brazil03$s2
brazil04$age<-brazil04$s2
brazil05$age<-brazil05$s7
brazil06$age<-brazil06$s7
brazil07$age<-brazil07$s11
brazil08$age<-brazil08$s9
brazil09$age<-brazil09$s6
brazil10$age<-brazil10$S8
brazil11$age<-brazil11$S17
brazil13$age<-brazil13$S11

lapop12$age<- lapop12$q2
lapop08$age<- lapop08$q2
lapop07$age<- lapop07$Q2
beps$age<- beps$q2
beps$age2<- beps$q2.x
beps$age3<- beps$q2.y
beps$age[is.na(beps$age)]<-beps$age2[is.na(beps$age)]
beps$age[is.na(beps$age)]<-beps$age3[is.na(beps$age)]

# Log municipal population

pop<-read.csv('muni.pop.2010.csv',fileEncoding='Latin1',as.is=T)
pop$codeibge<-substr(pop$muni,1,7)
br<-merge(br,pop[,c('pop','codeibge')])
br$log.muni.pop<-log(br$pop)
names(br)[which(names(br)=='codetse')]<-'tse_code'

brazil02<-merge(brazil02,br[c('tse_code','log.muni.pop')],all.x=T)
brazil03<-merge(brazil03,br[c('tse_code','log.muni.pop')],all.x=T)
brazil04<-merge(brazil04,br[c('tse_code','log.muni.pop')],all.x=T)
brazil05<-merge(brazil05,br[c('tse_code','log.muni.pop')],all.x=T)
brazil06<-merge(brazil06,br[c('tse_code','log.muni.pop')],all.x=T)
brazil07<-merge(brazil07,br[c('tse_code','log.muni.pop')],all.x=T)
brazil08<-merge(brazil08,br[c('tse_code','log.muni.pop')],all.x=T)
brazil09<-merge(brazil09,br[c('tse_code','log.muni.pop')],all.x=T)
brazil10<-merge(brazil10,br[c('tse_code','log.muni.pop')],all.x=T)
brazil11<-merge(brazil11,br[c('tse_code','log.muni.pop')],all.x=T)
brazil13<-merge(brazil13,br[c('tse_code','log.muni.pop')],all.x=T)

lapop07<-merge(lapop07,br[c('tse_code','log.muni.pop')],all.x=T)
lapop08<-merge(lapop08,br[c('tse_code','log.muni.pop')],all.x=T)
lapop12<-merge(lapop12,br[c('tse_code','log.muni.pop')],all.x=T)
beps<-merge(beps,br[c('tse_code','log.muni.pop')],all.x=T)

# News. When question is asked separately about different media, taking the max of all media. For 2008, which only asks "how do you inform yourself about politics," using a dummy for television, radio, newspaper, or Internet being mentioned. Standardizing within each year given the variation in question format and whether it specifies "political" news. 

standardize<-function(x) (x-mean(x,na.rm=T))/sd(x,na.rm=T)

# For the first two years, NA appears to be zero time a week (which should be a 1 on their scale)
brazil02$news<-standardize(apply(cbind(ifelse(is.na(brazil02$p54sta),1,unclass(brazil02$p54sta)), ifelse(is.na(brazil02$p54stb),1,unclass(brazil02$p54stb)),ifelse(is.na(brazil02$p54stb),1,unclass(brazil02$p54stb))),1,max,na.rm=T))
brazil03$news<-standardize(apply(cbind(ifelse(is.na(brazil03$p33sta),1,unclass(brazil03$p33sta)), ifelse(is.na(brazil03$p33stb),1,unclass(brazil03$p33stb)),ifelse(is.na(brazil03$p33stb),1,unclass(brazil03$p33stb))),1,max,na.rm=T))
brazil04$news<-standardize(apply(cbind(Recode(unclass(brazil04$p78sta),'9=1;c(1,10)=NA'), Recode(unclass(brazil04$p78stb),'9=1;c(1,10)=NA'), Recode(unclass(brazil04$p78stc),'9=1;c(1,10)=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
brazil05$news<-standardize(apply(cbind(Recode(unclass(brazil05$p62sta),'9=1;c(1,10)=NA'), Recode(unclass(brazil05$p62stb),'9=1;c(1,10)=NA'), Recode(unclass(brazil05$p62stc),'9=1;c(1,10)=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
brazil06$news<-standardize(apply(cbind(Recode(unclass(brazil06$p67st_a),'9=1;c(1,10)=NA'), Recode(unclass(brazil06$p67st_b),'9=1;c(1,10)=NA'), Recode(unclass(brazil06$p67st_c),'9=1;c(1,10)=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
brazil07$news<-standardize(apply(cbind(Recode(unclass(brazil07$p22sta),'9=1;c(1,10)=NA'), Recode(unclass(brazil07$p22stb),'9=1;c(1,10)=NA'), Recode(unclass(brazil07$p22stc),'9=1;c(1,10)=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
brazil08$news<-standardize(as.numeric((brazil08$p95st_e=='Menciona')|(brazil08$p95st_f=='Menciona')|(brazil08$p95st_g=='Menciona')|(brazil08$p95st_h=='Menciona')))
brazil09$news<-standardize(apply(cbind(Recode(unclass(brazil09$p91stm_a),'9=1;c(1,10)=NA'), Recode(unclass(brazil09$p91stm_b),'9=1;c(1,10)=NA'), Recode(unclass(brazil09$p91stm_c),'9=1;c(1,10)=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
brazil10$news<-standardize(apply(cbind(Recode(unclass(brazil10$P78ST_A),'96=0;c(0,98)=NA'), Recode(unclass(brazil10$P78ST_B),'96=0;c(0,98)=NA'), Recode(unclass(brazil10$P78ST_C),'96=0;c(0,98)=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
# For 2011, using radio, TV, newspaper, Internet, social media, cable TV, but not friends/family/informal channels.
brazil11$news<-standardize(apply(cbind(Recode(unclass(brazil11$P70ST_A),'1:4=NA'), Recode(unclass(brazil11$P70ST_B),'1:4=NA'), Recode(unclass(brazil11$P70ST_C),'1:4=NA'), Recode(unclass(brazil11$P70ST_D),'1:4=NA'), Recode(unclass(brazil11$P70N_E),'1:4=NA'), Recode(unclass(brazil11$P70N_G),'1:4=NA')),1,function(x) ifelse(sum(is.na(x))==length(x),NA,max(x,na.rm=T))))
brazil13$news<-standardize(Recode(unclass(brazil13$P73TGBNC),'4:5=NA;1=5'))

lapop07$news<-standardize(apply(cbind(Recode(unclass(lapop07$A1),'1=7;2=2;3:4=0'),Recode(unclass(lapop07$A2),'1=7;2=2;3:4=0'),Recode(unclass(lapop07$A3),'1=7;2=2;3:4=0'),Recode(unclass(lapop07$A4i),'1=7;2=2;3:4=0')),1,max,na.rm=T))
lapop08$news<-standardize(apply(cbind(Recode(unclass(lapop08$a1),'1=7;2=2;3:4=0'),Recode(unclass(lapop08$a2),'1=7;2=2;3:4=0'),Recode(unclass(lapop08$a3),'1=7;2=2;3:4=0'),Recode(unclass(lapop08$a4i),'1=7;2=2;3:4=0')),1,max,na.rm=T))
lapop12$news<-standardize(Recode(unclass(lapop12$gi0),'1=7;2=2;3=1;4:5=0'))
beps$news<-Recode(unclass(beps$gl0),'1=7;2=2;3=1;4:5=0;6:7=NA')
beps$news2<-Recode(unclass(beps$gl0.x),'1=7;2=2;3=1;4:5=0;6:7=NA')
beps$news[is.na(beps$news)]<-beps$news2[is.na(beps$news)]
beps$news<-standardize(beps$news)

# ==========================
# Control variables--elite
# ==========================

# Voter demand for clientelism
bls05$clientelism<-scale01(-1*as.vector(unclass(bls05$CLIENTS))) 
bls09$clientelism<-scale01(-1*as.vector(unclass(bls09$CLIENTS))) 
bls13$clientelism<-scale01(as.vector(unclass(bls13$CLIENTS))) 

# ========================================================
# Build dataset of dyads 
# ========================================================

elite.data.05<-bls05[,c('econ.sys','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','PP2005','PP2002','STATE','UNIQUE','YEARCASE','PPWEIGHT','age','donations.norm','years','clientelism')]
elite.data.09<-bls09[,c('econ.sys','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','PP2009','PP2006','STATE','UNIQUE','YEARCASE','PWEIGHT','age','donations.norm','years','clientelism')]
elite.data.13<-bls13[,c('econ.sys','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','PP2013','PP2010','STATE','UNIQUE','YEARCASE','PWEIGHT','age','donations.norm','years','clientelism')]
names(elite.data.05)[grep('PP2',names(elite.data.05))]<-c('partynow','partythen')
names(elite.data.09)[grep('PP2',names(elite.data.09))]<-c('partynow','partythen')
names(elite.data.13)[grep('PP2',names(elite.data.13))]<-c('partynow','partythen')
names(elite.data.05)[which(names(elite.data.05)=='PPWEIGHT')]<-'PWEIGHT'
elite.data.05$partynow<-as.character(elite.data.05$partynow)
elite.data.09$partynow<-as.character(elite.data.09$partynow)
elite.data.13$partynow<-as.character(elite.data.13$partynow)
elite.data.05$partythen<-as.character(elite.data.05$partythen)
elite.data.09$partythen<-as.character(elite.data.09$partythen)
elite.data.13$partythen<-as.character(elite.data.13$partythen)
elite.data.05$STATE<-as.character(elite.data.05$STATE)
elite.data.09$STATE<-as.character(elite.data.09$STATE)
elite.data.13$STATE<-as.character(elite.data.13$STATE)
# Killing factor levels that for some reason survived recode
elite.data.05$econ.sys<-as.vector(elite.data.05$econ.sys)
elite.data.05$auth<-as.vector(elite.data.05$auth)
elite.data.13$econ.sys<-as.vector(elite.data.13$econ.sys)

elite.data<-rbind(elite.data.05,elite.data.09,elite.data.13)
names(elite.data)<-c('elite_econ','elite_ideol','elite_gaymarriage','elite_affirm','elite_classaffirm','elite_environment','elite_abortion','elite_evang','elite_loweduc','elite_black','elite_female','elite_partythen','elite_partynow','elite_uf','elite_id','elite_yearid','elite_pweight','elite_age','elite_donations.norm','elite_years','elite_clientelism')

elite.data$elite_year<-as.numeric(substr(elite.data$elite_yearid,1,4))
elite.yweight<-data.frame((1/3)/prop.table(table(elite.data$elite_year)))
names(elite.yweight)<-c('elite_year','elite_yweight')
elite.data<-merge(elite.data,elite.yweight)

mass.data.05<-rbind(
brazil02[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil03[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil04[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil05[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')])
mass.data.09<-rbind(
brazil06[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil07[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil08[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil09[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
lapop07[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
lapop08[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')])
mass.data.13<-rbind(
brazil10[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil11[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
brazil13[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
beps[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')],
lapop12[,c('econfact','ideol','gaymarriage','affirm','class_affirm','environment','abortion','evangelical','low.educ','black','female','party','uf','unique','blsyear','age','log.muni.pop','news','pweight')])
mass.data<-rbind(mass.data.05,mass.data.09,mass.data.13)
names(mass.data)<-c('mass_econ','mass_ideol','mass_gaymarriage','mass_affirm','mass_classaffirm','mass_environment','mass_abortion','mass_evang','mass_loweduc','mass_black','mass_female','mass_party','mass_uf','mass_id','mass_blsyear','mass_age','mass_log.muni.pop','mass_news','mass_pweight')

mass.yweight<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear)))
names(mass.yweight)<-c('mass_blsyear','mass_yweight')
mass.data<-merge(mass.data,mass.yweight)

# Separate set of year weights for those years when race was asked
mass.yweight.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_black)])))
names(mass.yweight.black)<-c('mass_blsyear','mass_yweight.black')
mass.data<-merge(mass.data, mass.yweight.black, all.x=T)
mass.data$mass_yweight.black[is.na(mass.data$mass_black)]<-NA

save(elite.data,file='elite.data.RData')
save(mass.data,file='mass.data.RData')

dyads<-rbind(expand.grid(YEARCASE=elite.data.05$YEARCASE, unique=mass.data.05$unique, stringsAsFactors=F),
expand.grid(YEARCASE=elite.data.09$YEARCASE, unique=mass.data.09$unique, stringsAsFactors=F),
expand.grid(YEARCASE=elite.data.13$YEARCASE, unique=mass.data.13$unique, stringsAsFactors=F))
names(dyads)<-c('elite_yearid','mass_id')
dyads<-merge(dyads,elite.data)
dyads<-merge(dyads,mass.data)

dyads.yweight<-data.frame((1/3)/prop.table(table(dyads$elite_year)))
names(dyads.yweight)<-c('elite_year','dyads_yweight')
dyads<-merge(dyads,dyads.yweight)

# Separate set of year weights for those years when race was asked
dyads.yweight.black<-data.frame((1/2)/prop.table(table(dyads$mass_blsyear[!is.na(dyads$mass_black)])))
names(dyads.yweight.black)<-c('mass_blsyear','dyads_yweight.black')
dyads$dyads_yweight.black<-dyads.yweight.black$dyads_yweight.black[match(dyads$mass_blsyear, dyads.yweight.black$mass_blsyear)]
dyads$dyads_yweight.black[is.na(dyads$mass_black)]<-NA

save(dyads,file='dyads.RData')

# =================
# Appendix Figure 1 
# =================

pdf(file='rollcall.pdf',height=6,width=6)
par(mar=c(6,4,1,0)+.1)
rollcall.plot<-barplot(cbind(tapply(bls13$environment, bls13$forest.vote, mean, na.rm=T), tapply(bls13$affirm, bls13$quota.vote, mean, na.rm=T)),beside=T,ylim=c(0,1),ylab='Issue Attitude (Left-Right)')
Axis(side=1,at=as.vector(rollcall.plot),labels=c('Yes','No','Yes','No'),tick=F,line=-.5)
Axis(side=1,at=apply(rollcall.plot,2,mean),labels=c('Forest Code Vote','Affirmative Action Vote'),tick=F,line=1)
Axis(side=1,at=mean(rollcall.plot),labels='NOTE: Issue attitudes are regarding environmentalism for the Forest Code vote and\nrace-based quotas for the affirmative action vote. See main text for details on each bill.',tick=F,line=3.5,cex.axis=.75)
dev.off()

# ==========================
# Results Conveyed Textually 
# ==========================

# "Across all surveys that measure interest in politics, our news variable is positively correlated with political interest at r = 0.17."

brazil03$interest<-scale01(-1*ifelse(unclass(brazil03$p61st) %in% c(6),NA,as.vector(unclass(brazil03$p61st))))
brazil04$interest<-scale01(-1*ifelse(unclass(brazil04$p29st) %in% c(1,6),NA,as.vector(unclass(brazil04$p29st))))
brazil05$interest<-scale01(-1*ifelse(unclass(brazil05$p37st) %in% c(1,6),NA,as.vector(unclass(brazil05$p37st))))
brazil07$interest<-scale01(-1*ifelse(unclass(brazil07$p59st) %in% c(1,6),NA,as.vector(unclass(brazil07$p59st))))
brazil09$interest<-scale01(-1*ifelse(unclass(brazil09$p32st) %in% c(1,6),NA,as.vector(unclass(brazil09$p32st))))
brazil10$interest<-scale01(-1*ifelse(unclass(brazil10$P23ST) %in% c(1,6),NA,as.vector(unclass(brazil10$P23ST))))
brazil13$interest<-scale01(-1*ifelse(unclass(brazil13$P20STGBS) %in% c(4),NA,as.vector(unclass(brazil13$P20STGBS))))
lapop07$interest<-scale01(-1*as.vector(unclass(lapop07$POL1)))
lapop08$interest<-scale01(-1*as.vector(unclass(lapop08$pol1)))
lapop12$interest<-scale01(-1*as.vector(unclass(lapop12$pol1)))

beps$interest<-scale01(-1*ifelse(unclass(beps$pol1) > 4, NA,as.vector(unclass(beps$pol1))))
beps$interest2<-scale01(-1*ifelse(unclass(beps$pol1.x) > 4, NA,as.vector(unclass(beps$pol1.x))))
beps$interest3<-scale01(-1*ifelse(unclass(beps$pol1.y) > 4, NA,as.vector(unclass(beps$pol1.y))))
beps$interest[is.na(beps$interest)]<-beps$interest2[is.na(beps$interest)]
beps$interest[is.na(beps$interest)]<-beps$interest3[is.na(beps$interest)]

cor.test(c(brazil03$interest, brazil04$interest, brazil05$interest, brazil07$interest, brazil09$interest, brazil10$interest, brazil13$interest, lapop07$interest, lapop08$interest, lapop12$interest, beps$interest),c(brazil03$news, brazil04$news, brazil05$news, brazil07$news, brazil09$news, brazil10$news, brazil13$news, lapop07$news, lapop08$news, lapop12$news, beps$news))
